/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package untils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;


/**
 *
 * @author Kanet
 */
public class DoiTuong {
    String ten;
    ArrayList<Double> giaTri;
    
    public DoiTuong(){
        ten="";
        giaTri=new ArrayList<>();
    }
    public DoiTuong(DoiTuong x){
        ten=x.ten;
        giaTri=new ArrayList<>(x.giaTri);
    }
     public DoiTuong(String ten,int size){
        this.ten=ten;
        this.giaTri=new ArrayList<>();
        for(int i=0;i<size;i++)
            this.giaTri.add(0.0);
    }
    public DoiTuong(String ten,ArrayList<Double> giaTri){
        this.ten=ten;
        this.giaTri=giaTri;
    }
    double KhoangCach(DoiTuong x) {
        double kc=0;
        for(int i=0;i<x.giaTri.size();i++)
            kc+=Math.pow(x.giaTri.get(i)-this.giaTri.get(i), 2);
        return Math.sqrt(kc);
    }
    
    void TinhTrungBinh(int mau){
        for(int i=0;i<this.giaTri.size();i++){
            double temp=this.giaTri.get(i)/mau;
            this.giaTri.set(i, temp);
        }
    }
    void PhucHoiTrungTam(){
        for(int i=0;i<this.giaTri.size();i++)
            this.giaTri.set(i,0.0);
    }
    void TongDoiTuong(DoiTuong x){
        for(int i=0;i<x.giaTri.size();i++){
            double temp=this.giaTri.get(i)+x.giaTri.get(i);
            this.giaTri.set(i, temp);
        }
    }
    int DocDuLieu(String line){
        String []s=line.split(",");
        int i=0;
        if(DoiTuong.isNumber(s[0])==false){
            this.ten=s[i];
            i++;
        }            
        for(;i<s.length;i++){
            if(DoiTuong.isNumber(s[i])==true){
                double value=Double.parseDouble(s[i]);
                giaTri.add(value);
            }else{
                return -1;
            }
        }     
       
        return giaTri.size();
    }
    public boolean SoSanh(DoiTuong x){
        for(int i=0;i<x.giaTri.size();i++)
            if(x.giaTri.get(i).compareTo(this.giaTri.get(i))!=0)
                return false;
        return true;
    }
    void GhiTapTin(BufferedWriter bw){
        try {
            bw.write(this.ten);
            for(int i=0;i<this.giaTri.size();i++)
                bw.write(","+this.giaTri.get(i).toString());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public static boolean isNumber(String str){
        try{
            Double.parseDouble(str);
        }catch(NumberFormatException nfe){
            return false;
        }            
        return true;
    }
   
}
